package com.college.userInfo.service.impl;

import com.college.common.pojo.ao.User;
import com.college.common.pojo.vo.PageVo;
import com.college.userInfo.mapper.UserMapper;
import com.college.userInfo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询一个用户的公开信息
     *
     * @param id
     * @return
     */
    @Override
    public User getUser(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 登录，安全问题未解决
     *
     * @param accountNumber
     * @param userPassword
     * @return
     */
    @Override
    public User userLogin(String accountNumber, String userPassword) {
        return userMapper.userLogin(accountNumber, userPassword);
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @Override
    public boolean userSignIn(User user) {
        return userMapper.insert(user) == 1;
    }

    /**
     * 修改用户公开信息，未验证用户身份
     *
     * @param user
     * @return
     */
    @Override
    public boolean updateUserInfo(User user) {
        return userMapper.updateByPrimaryKeySelective(user) == 1;
    }

    /**
     * 修改密码
     *
     * @param newPassword
     * @param oldPassword
     * @param id
     * @return
     */
    @Override
    public boolean updatePassword(String newPassword, String oldPassword, Long id) {
        return userMapper.updatePassword(newPassword, oldPassword, id) == 1;
    }

    @Override
    public PageVo<User> getUserByStatus(int status, int page, int nums) {
        List<User> list;
        int count = 0;
        if (status == 0) {
            count = userMapper.countNormalUser();
            list = userMapper.getNormalUser((page - 1) * nums, nums);
        } else {
            count = userMapper.countBanUser();
            list = userMapper.getBanUser((page - 1) * nums, nums);
        }
        return new PageVo<>(list, count);
    }


    /**
     * 通过用户的姓名查询用户的id
     */
    @Override
    public Long getUserId(String nickname) {
        return userMapper.selectByUserName(nickname);
    }


    /**
     * 通过用户账号查询用户信息
     *
     * @return
     */
    @Override
    public PageVo<User> getUserByNumber(String searchValue, int mode) {
        List<User> list;
        list = userMapper.getUserByNumber(searchValue, mode - 1);
        return new PageVo<>(list, 1);
    }

}
